/*
	This file is part of Kraft der Mada.
	Copyright (c) 2009 Daniel Wickert

    Kraft der Mada is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Kraft der Mada is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kraft der Mada. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdmadainc.h"

#include "core/Factory.h"

#include "core/Rtti.h"

namespace mada
{
	Factory* Factory::ms_instance = NULL;

	Factory::Factory() : m_rttis()
	{
	}

	Factory::~Factory()
	{
		m_rttis.clear();
	}

	Factory* Factory::instance()
	{
		if (ms_instance == NULL)
		{
			ms_instance = new Factory();
		}

		return ms_instance;
	}

	void Factory::registerClass(const Rtti* rtti)
	{
		m_rttis.insert(std::make_pair(rtti->getName(), rtti));
	}

	bool Factory::isClassRegistered(const String& className) const
	{
		return m_rttis.find(className) != m_rttis.end();
	}

	const Rtti* Factory::getClassRtti(const String& className) const
	{
		RttiMap::const_iterator it = m_rttis.find(className);
		return it->second;
	}

	RefCounted* Factory::createClassInstance(const String& className) const
	{
		RttiMap::const_iterator it = m_rttis.find(className);
		return it->second->createInstance();
	}
}
